from django.shortcuts import render
from django.http import HttpResponse
#CBV 引入
from django.views import View
#from systemapp.models import *
from systemapp import *
# Create your views here.
import uuid

def deptlist(request):
	obj = list(models.Org_Department.objects.all());
	print(obj)

	for x in obj:
		if x.Level == 0:
			print(x.DepartmentName)

	return render(request, 'systemapp/deptindex.html', {
		'deptlist': obj
	})



class Role(View):
	def get(self, request):
		obj = list(models.Org_Role.objects.all())
		
		return render(request, 'systemapp/roleindex.html', {
			'rolelist': obj
		})



class User(View):
	def get(self, request):

		#获取角色信息，放到下拉列表中
		rolelist = list(models.Org_Role.objects.all())

		#获取部门信息，放到下来列表中
		departmentlist = list(models.Org_Department.objects.all())

		return render(request, 'systemapp/userindex.html', {
			'rolelist': rolelist,
			'departmentlist': departmentlist,
		})

	def post(self, request):
		employeename = request.POST.get('employee_name');
		employeeno = request.POST.get('employee_no');
		userpassword = request.POST.get('user_password');
		employeeemail = request.POST.get('employee_email');
		departmentKeyId = request.POST.get('employee_department');
		roleKeyId = request.POST.get('employee_role');

		new_user_keyid = uuid.uuid1();
		new_employee_keyid = uuid.uuid1();
		new_user = {
			'KeyId': new_user_keyid,
			'UserName' : employeename,
			'Password' : userpassword,
			'CreateUserKeyId' : '99999999-9999-9999-9999-999999999999',
			'UpdateUserKeyId' : '99999999-9999-9999-9999-999999999999',
			'Version' : 0,
			'IsDelete' : 0
		}
		new_employee = {
			'KeyId': new_employee_keyid,
			'EmployeeNo' : '',
			'EmployeeName' : employeename,
			'UserKeyId' : new_user_keyid,
			'Mobile' : '',
			'Email' : employeeemail,
			'CreateUserKeyId' : '99999999-9999-9999-9999-999999999999',
			'UpdateUserKeyId' : '99999999-9999-9999-9999-999999999999',
			'Version' : 0,
			'IsDelete' : 0,
		}
		new_user_department_role_Relations = {
			'KeyId' : uuid.uuid1(),
			'UserKeyId' : new_user_keyid,
			'EmployeeKeyId' : new_employee_keyid,
			'RoleKeyId' : roleKeyId,
			'DepartmentKeyId' : departmentKeyId,
		}
		print(123)
		userobj = models.Org_User(new_user);
		employeeobj = models.Org_Employee(new_employee)
		print(123)
		raleobj = models.Org_UserDeptRoleRelations(new_user_department_role_Relations)
		userobj.save()
		employeeobj.save()
		raleobj.save()

		#新增员工要写 三个表， 员工表，用户表，员工角色部门关系表
		print(uuid.uuid1())
		print(employeename)
		print(employeeno)
		print(userpassword)
		print(employeeemail)
		print(departmentKeyId)
		print(roleKeyId)


		return HttpResponse('OK');

class UserDetail(View):

	def get(self, request):

			

		return render(request, 'userdetail.html', {

		})

class RoleDetails(View):
	def get(self, request):
		role_keyid = request.GET.get('role_keyid')

		return render(request, 'systemapp/roledetails.html', {
			'role_keyid': role_keyid
		})